import { Component } from '@angular/core';
import { LoadingController, ToastController } from '@ionic/angular';

/**
 * UI 层公用方法的抽象类
 *
 * @export
 * @abstract
 * @class BaseUI
 */
export abstract class BaseUI {

    isLoading = false;

    constructor() { }

    /**
     * 通用的展示loading(老版本)
     *
     * @protected
     * @param {LoadingController} loadingCtrl
     * @param {*} message
     * @returns
     * @memberof BaseUI
     */
    protected async showLoading(loadingCtrl: LoadingController, message: any) {
        const loader = await loadingCtrl.create({
            message
        });
        await loader.present();
        return loader;
    }

    /**
     * 通用的展示loading
     *
     * @protected
     * @param {string} message
     * @param {LoadingController} loadingCtrl
     * @returns
     * @memberof BaseUI
     */
    protected async present(message: string, loadingCtrl: LoadingController) {
        this.isLoading = true;
        return await loadingCtrl.create({
            message,
            // duration: 2000,
        }).then(a => {
            a.present().then(() => {
                console.log('presented');
                if (!this.isLoading) {
                    a.dismiss().then(() => console.log('abort presenting'));
                }
            });
        });
    }

    /**
     * 取消loading
     *
     * @param {LoadingController} loadingCtrl
     * @returns
     * @memberof BaseUI
     */
    async dismiss(loadingCtrl: LoadingController) {
        this.isLoading = false;
        return await loadingCtrl.dismiss().then(() => console.log('dismissed'));
    }

    /**
     * 通用的toast展示
     *
     * @protected
     * @param {ToastController} toastCtrl
     * @param {string} message
     * @returns
     * @memberof BaseUI
     */
    protected async showToast(toastCtrl: ToastController, message: string) {
        const toast = await toastCtrl.create({
            message,
            duration: 2000,  // 默认展示的时长
            position: 'bottom'
        });
        await toast.present();
        return toast;
    }

}